package com.genia.toolbox.security.annotation.ws_interceptor;

import org.apache.cxf.interceptor.Fault;
import org.apache.cxf.message.Message;
import org.apache.cxf.phase.AbstractPhaseInterceptor;
import org.apache.cxf.phase.Phase;

import com.genia.toolbox.basics.tools.shared.PropertiesClass;

/**
 * an annotation used to log all XML inflows. You have just to add this line
 * "@InInterceptors(interceptors = "
 * com.genia.toolbox.projects.sample_ws_trampo.war
 * .ws.interceptor.CatchInInterceptor")" under the webservice implementation
 */
public class CatchInInterceptor extends AbstractPhaseInterceptor<Message> {

	private int limit = 100 * 1024;

	/**
	 * Constructor, simply delegates to superclass.
	 */
	public CatchInInterceptor() {
		super(Phase.RECEIVE);
	}

	/**
	 * Constructor, simply delegates to superclass.
	 *
	 * @param phase
	 *            The phase used to configure the direction of the receive
	 *            message
	 */
	public CatchInInterceptor(String phase) {
		super(phase);
	}

	/**
	 * Constructor, simply delegates to superclass.
	 *
	 * @param lim
	 *            The limit used to set the reception's limit of the message
	 */
	public CatchInInterceptor(int lim) {
		this();
		limit = lim;
	}

	/**
	 * setter of the limit property
	 *
	 * @param lim
	 */
	public void setLimit(int lim) {
		limit = lim;
	}

	/**
	 * getter of the limit property
	 *
	 * @return
	 */
	public int getLimit() {
		return limit;
	}

	/**
	 * delegate the handling to the parent and then silently ignore exception
	 * that must be accepted.
	 *
	 * @param msg
	 *            the message being processed
	 * @throws Fault
	 *             if an error occured
	 */
	public void handleMessage(Message message) throws Fault {
		if (PropertiesClass.getActiveWsInterceptor()) {
			ReadInInterceptor rii = new ReadInInterceptor(message, limit);
			rii.logging();
		}
	}
}